﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Linq;
using Marx.Utils;
using ET;

namespace Marx
{

    public class ServerConfigration : Singleton<ServerConfigration>
    {
        public ServerConfigData Data { get; private set; }
        public ConfigDB DB => Data.DB;
        public ConfigPlayer Player => Data.Player;
        public ConfigServices Services => Data.Services;

        public Dictionary<string, ConfigService> ServiceDict { get; private set; }

        public void Initializer(string path)
        {
            try
            {
                using (StreamReader reader = new StreamReader(path))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(ServerConfigData));
                    Data = serializer.Deserialize(reader) as ServerConfigData;
                }
                ServiceDict = Data.Services.Items.ToDictionary(i => i.Name);
            }
            catch (Exception e)
            {
                Log.Error($"Load server config failed, path : [{path}]");
                throw e;
            }
        }

        public ConfigService GetServiceConfig(string name)
        {
            return ServiceDict.GetOrDefault(name);
        }
    }
}
